<!-- register.html -->
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Register</title>
    <style>
        body {
            background-color: #f2f2f2;
            font-family: Arial, sans-serif;
        }

        body {

            background-size: 100% auto;
            background-image: url("../static/test3.gif");
        }

        .container {
            width: 300px;
            margin: 0 auto;
            margin-top: 280px;
            padding: 20px;
            background-color: #ffffff;
            border-radius: 5px;
            box-shadow: 0 2px 4px rgba(3, 9, 2, 0.1);
        }

        h2 {
            text-align: center;
            margin-bottom: 30px;
        }

        input[type="text"],
        input[type="password"] {
            width: 90%;
            padding: 10px;
            margin-bottom: 20px;
            border: 1px solid #ccc;
            border-radius: 3px;
        }

        button {
            width: 100%;
            padding: 10px;
            background-color: #4CAF50;
            color: #ffffff;
            font-size: 16px;
            border: none;
            border-radius: 3px;
            cursor: pointer;
        }

        button:hover {
            background-color: #45a049;
        }

        .switch-btn {
            text-align: center;
            margin-top: 10px;
        }

        .switch-btn a {
            color: #0066cc;
            text-decoration: none;
        }
    </style>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<div class="container">
    <h2>Register</h2>
    <form id="RegisterForm" method="post" th:action="@{/user/register}">
        <div>
            <input type="text" id="phoneNumber" name="phoneNumber" placeholder="PhoneNumber" required>
        </div>
        <div>
            <input type="text" id="username" name="username" placeholder="Username" required>
        </div>
        <div>
            <input type="password" id="password" name="password" placeholder="Password" required>
        </div>
        <div>
            <button type="submit">Register</button>
        </div>
        <div class="hidden-div" th:text="${Authorization}"> </div>
    </form>
    <div class="switch-btn">
        <a href="/thymeleaf/">Already have an account? Login</a>
    </div>
</div>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
    $(document).ready(function () {
        // 当按钮被点击时
        $("#registerButton").click(function () {
            // 获取表单中的数据
            var phoneNumber = $("#phoneNumber").val();
            var username = $("#username").val();
            var password = $("#password").val();
            var authorizationData = $(".hidden-div").text();
            // 构建要发送的数据
            var data = {
                Authorization: authorizationData,
                phoneNumber: phoneNumber,
                username: username,
                password: password
            };

            // 发送 POST 请求到 /user/register
            $.ajax({
                type: "POST",
                url: "/user/register",
                //contentType: "application/json;charset=UTF-8", // 指定请求体的内容类型为 JSON
                data: data,
                //dataType: 'json',// 将数据对象转换为 JSON 字符串
                success: function (response) {
                    // 处理成功响应
                    console.log("Registration successful!");
                    location.reload();
                    // 这里可以根据响应的内容执行进一步的操作
                },
                error: function () {
                    // 处理错误响应
                    console.log("Registration failed!");
                    // 这里可以根据错误情况执行相应的操作
                }
            });
        });
    });
</script>
</body>
</html>